package models

import (
	setting "common/viper"
	"github.com/gin-gonic/gin"
	"math/rand"
	"strconv"
	"time"
)

const (
	HttpVideo   = "/v1/video/down?video_id="
	HttpImg     = "/v1/img/down?img_id="
	SplitFormat = "|"
	LevelOne    = 2
)

// GetPageInfo 处理分页请求的参数
func GetPageInfo(c *gin.Context) (page, page_size int, err error) {
	//获得分页参数offer和limit
	pageStr := c.Query("page")
	page_sizeStr := c.Query("size")
	page, err = strconv.Atoi(pageStr)
	if err != nil {
		page = 1
	}
	page_size, err = strconv.Atoi(page_sizeStr)
	if err != nil {
		page_size = 10
	}
	return
}
func GetRandomInt(size, bigEnd int, oldNums []int) []int {
	// 初始化随机数生成器
	rand.Seed(time.Now().UnixNano())

	// 创建一个包含1到bigEnd的切片
	availableNums := make([]int, bigEnd)
	for i := 0; i < bigEnd; i++ {
		availableNums[i] = i + 1
	}

	// 从availableNums中移除oldNums中的数字
	for _, num := range oldNums {
		for i := 0; i < len(availableNums); i++ {
			if availableNums[i] == num {
				availableNums = append(availableNums[:i], availableNums[i+1:]...)
				break
			}
		}
	}

	// 检查是否有足够的数字可供生成
	if len(availableNums) < size {
		return []int{}
	}

	// 生成随机不重复的数字
	result := make([]int, size)
	for i := 0; i < size; i++ {
		index := rand.Intn(len(availableNums))
		result[i] = availableNums[index]
		availableNums = append(availableNums[:index], availableNums[index+1:]...)
	}

	return result
}

// 判断切片中是否包含某个元素
func contains(slice []int, element int) bool {
	for _, value := range slice {
		if value == element {
			return true
		}
	}
	return false
}

// GetHttpUrl 拼接链接
func GetHttpUrl(uid, path string) string {
	return setting.Conf.ServerInfo.Addr + path + uid
}

// removeListToOne 删除指定位置的元素
func RemoveList(target string, nums []string) []string {
	length := len(nums)
	index := -1
	for i, v := range nums {
		if v == target {
			index = i
			break
		}
	}
	if index != -1 {
		copy(nums[index:], nums[index+1:])
	}
	return nums[:length-1]
}
